import CryptoJS from 'crypto-js';

// 加密
function encrypt(data, AES_KEY, IV) {
    AES_KEY = AES_KEY ? AES_KEY : "JGPcYXIO2TY0Vtav" //加密密钥
    IV = IV ? IV : "0000000000000000" //加密aesIV，偏移量
    const key = CryptoJS.enc.Utf8.parse(AES_KEY);
    const iv = CryptoJS.enc.Utf8.parse(IV);
    const encrypted = CryptoJS.AES.encrypt(data, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding,
    });
    return encrypted.toString();
}

// 解密  data：要加密解密的数据，AES_KEY：密钥，IV:偏移量
function decrypt(data, AES_KEY, IV) {
    AES_KEY = AES_KEY ? AES_KEY : "JGPcYXIO2TY0Vtav" //加密密钥
    IV = IV ? IV : "0000000000000000" //加密aesIV，偏移量
    const key = CryptoJS.enc.Utf8.parse(AES_KEY);
    const iv = CryptoJS.enc.Utf8.parse(IV);
    const decrypt = CryptoJS.AES.decrypt(data, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding,
    }).toString(CryptoJS.enc.Utf8);
    return decrypt;
}

export { decrypt, encrypt }